import {
  queryAllOrder,
  queryDeliverOps,
  sendDeliver,
  addRemark,
  closeOrder,
  getOcr,
  settleOrder,
  exportOrder,
  getUserIdCardPhotoInfo,
  businessFindBuyOutOrder,
  businessBuyOutOrder,
  rejectOrder,
  viewRejectDetail,
  getCardinformation
} from '@/services/api';
import { message } from 'antd';

export default {
  namespace: 'order',
  state: {
    queryParams: {
      userName: null,
      addressUserName: null,
      addressUserPhone: null,
      orderId: null,
      prodcutName: null,
      productId: null,
      subStatus: null,
      telephone: null,
      time: null,
      createTime: null,
      status: '',
      pageNumber: 1,
      pageSize: 10,
    },
    total: 0,
    list: [],
    modalVisible: false,
    modalStyle: 'deliver',
    deliverOptions: [],
    idCardNo: {},
    reportDetal: {},
    buyoutPrice: null,
    picturesList: [],
    ocr: {},
    rejectDetail: {}
  },

  effects: {
    *queryAllOrder({ payload }, { call, put }) {
      if (payload.status == 'USER_APPLICATION') {
        payload.status = 'WAITING_BUSINESS_DELIVERY',
          payload.examineStatus = 1
      }
      const response = yield call(queryAllOrder, payload);
      if (payload.examineStatus) {
        payload.status = 'USER_APPLICATION'
      }
      const { total, records } = response.data;
      yield put({
        type: 'save',
        payload: {
          queryParams: payload,
          list: records,
          total,
        },
      });
    },
    *queryDeliverOps({ payload }, { call, put }) {
      const response = yield call(queryDeliverOps);
      yield put({
        type: 'save',
        payload: {
          deliverOptions: response.data,
        },
      });
    },
    *businessFindBuyOutOrder({ payload }, { call, put }) {
      const response = yield call(businessFindBuyOutOrder, payload);
      yield put({
        type: 'save',
        payload: {
          buyoutPrice: response.data,
        },
      });
    },
    *businessBuyOutOrder({ payload }, { call, put }) {
      const response = yield call(businessBuyOutOrder, payload);
      if (response && response.code === 1) {
        message.success(response.data);
        yield put({
          type: 'save',
          payload: {
            modalVisible: false,
          },
        });
      }
    },
    *addRemark({ payload }, { call, put, select }) {
      const params = yield select(state => state.order.queryParams);
      const res = yield call(addRemark, payload);
      if (res.code) {
        message.success('备注成功');
      }
      yield put({
        type: 'save',
        payload: {
          modalVisible: false,
        },
      });
      yield put({
        type: 'queryAllOrder',
        payload: params,
      });
    },

    *sendDeliver({ payload }, { call, put, select }) {
      const params = yield select(state => state.order.queryParams);
      yield call(sendDeliver, payload);
      message.success('操作成功');
      yield put({
        type: 'save',
        payload: {
          modalVisible: false,
        },
      });
      yield put({
        type: 'queryAllOrder',
        payload: params,
      });
    },

    *closeOrder({ payload }, { call, put, select }) {
      const params = yield select(state => state.order.queryParams);
      const res = yield call(closeOrder, payload);
      if (res && res.code) {
        message.success('关闭成功');
      }
      yield put({
        type: 'queryAllOrder',
        payload: params,
      });
    },

    *settleOrderOver({ payload, callback }, { call, put, select }) {
      const params = yield select(state => state.order.queryParams);
      const res = yield call(settleOrder, payload);
      callback(res)
      if (res && res.code > 0) {
        message.success('操作成功');
        yield put({
          type: 'save',
          payload: {
            modalVisible: false,
          },
        });
        yield put({
          type: 'queryAllOrder',
          payload: params,
        });
      }
    },

    *exportOrderList({ payload }, { call }) {
      const res = yield call(exportOrder, payload);

      if (res && res.code > 0) {
        window.open(res.data);
        setTimeout(() => {
          message.success('操作成功');
        }, 1500);
      }
    },

    *getUserIdCardPhotoInfo({ payload, callback }, { call, put }) {
      const res = yield call(getUserIdCardPhotoInfo, payload);
      callback(res)
      if (res && res.code > 0) {
        yield put({
          type: 'save',
          payload: {
            idCardNo: res.data,
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            idCardNo: '暂未上传身份信息，请联系用户上传',
          },
        });
      }
    },

    //商家拒绝订单
    *rejectOrder({ payload, callback }, { call, put }) {
      let res = yield call(rejectOrder, payload)
      callback(res)
    },

    //查看拒绝订单详情
    *viewRejectDetail({ payload, callback }, { call, put }) {
      let res = yield call(viewRejectDetail, payload)
      callback(res)
      if (res.code) {
        yield put({
          type: 'saveRejectDetail',
          payload: res.data
        })
      }
    },
    //获取ocr
    *ocr({ payload, callback }, { call, put }) {
      let res = yield call(getOcr, payload)
      if (res.code) {
        yield put({
          type: 'saveOcr',
          payload: res.data
        })
      }
      callback(res)
    },
    //获取证件信息
    *getCardinformation({ payload, callback }, { call, put }) {
      const res = yield call(getCardinformation, payload);
      if (res.code == '1') {
        callback(res.data)
      }
    }
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveOcr(state, payload) {
      let _state = JSON.parse(JSON.stringify(state))
      _state.ocr = payload.payload
      return _state
    },
    saveRejectDetail(state, payload) {
      let _state = JSON.parse(JSON.stringify(state))
      _state.rejectDetail = payload.payload
      return _state
    },
    changeKey(state) {
      let _state = JSON.parse(JSON.stringify(state))
      _state.queryParams.status = 'USER_APPLICATION'
      console.log(1111)
      return _state
    }
  },
};

/**
 * 订单号  orderId
商品主图  images
商品名称  productName
规格 skuTitle
单价 rent
天数 renDuration
数量 nums
实付款 realRent
状态 status
telephone 买家
 */
